package autokeyboard.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import autokeyboard.data.ComboBoxModel;
import autokeyboard.data.PicturePanel;
import autokeyboard.data.RobotData;
import autokeyboard.handler.StartOptionEvent;

import com.melloware.jintellitype.JIntellitype;

@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public class StartOptionDialog extends JDialog
{

	private JPanel				panelOption		= new JPanel();
	private JPanel				panelButton		= new JPanel();

	private JPanel				panelChoice1	= new JPanel();
	private JPanel				panelChoice2	= new JPanel();

	private JButton				buttonStart		= new JButton();
	private JButton				buttonCancel	= new JButton();

	private JRadioButton		rdbtUseHotKey	= new JRadioButton();
	private JRadioButton		rdbtUseTimer	= new JRadioButton();
	private ButtonGroup			group			= new ButtonGroup();

	private JLabel				lblStartKey		= new JLabel();
	private JLabel				lblPauseKey		= new JLabel();
	private JLabel				lblStopKey		= new JLabel();
	private JLabel				lblStartAfter	= new JLabel();

	private JComboBox			cbbStartKey		= new JComboBox();
	private JComboBox			cbbPauseKey		= new JComboBox();
	private JComboBox			cbbStopKey		= new JComboBox();
	private JComboBox			cbbStartAfter	= new JComboBox();

	private ComboBoxModel		model			= new ComboBoxModel();

	private StartOptionEvent	event			= new StartOptionEvent(lblStartKey, lblPauseKey, lblStopKey, lblStartAfter, cbbStartKey, cbbPauseKey, cbbStopKey, cbbStartAfter);
	private RobotData			data			= new RobotData();

	public static String[]		listHostKey		= { "F1", "F2", "F3" };
	public static boolean		isStartPressed	= false;
	
	private PicturePanel picPanel = new PicturePanel();
	public static boolean isNewPic = false;
	private JFrame frame;

	public StartOptionDialog(JFrame frame)
	{
		super(frame, true);
		this.frame = frame;
		initComponent();

		this.setPreferredSize(new Dimension(700, 210));
		this.setMinimumSize(new Dimension(700, 210));
		this.setResizable(false);
		this.setTitle("Start Auto Keyboard");
		this.setLocationRelativeTo(null);
		this.setIconImage(new ImageIcon(getClass().getResource("/Small_Icons/Icon.png")).getImage());
	}

	private void initComponent()
	{
		setLabelProperties();
		setComboBoxProperties();
		setRadioButtonProperties();
		setButtonProperties();
		setComboBoxModel();

		setLayoutPanelOption();
		setLayoutPanelButton();
		setLayoutPanelChoice1();
		setlayoutPanelChoice2();
		setMainLayout();

		buttonEvent();
		keyEvent();
		comboboxEvent();
		radioButtonEvent();
	}

	private void setLabelProperties()
	{
		lblStartKey.setFont(new Font("Calibri", 1, 20));
		lblStartKey.setForeground(Color.yellow);
		lblStartKey.setText("Start key");

		lblPauseKey.setFont(lblStartKey.getFont());
		lblPauseKey.setForeground(lblStartKey.getForeground());
		lblPauseKey.setText("Pause key");

		lblStopKey.setFont(lblStartKey.getFont());
		lblStopKey.setForeground(lblStartKey.getForeground());
		lblStopKey.setText("Stop key");

		lblStartAfter.setFont(lblStartKey.getFont());
		lblStartAfter.setForeground(lblStartKey.getForeground());
		lblStartAfter.setText("Start after");
	}

	private void setComboBoxProperties()
	{
		cbbStartKey.setFont(new Font("Calibri", 0, 20));
		cbbPauseKey.setFont(cbbStartKey.getFont());
		cbbStopKey.setFont(cbbStartKey.getFont());
		cbbStartAfter.setFont(cbbStartKey.getFont());

		cbbStartKey.setPreferredSize(new Dimension(80, 35));
		cbbPauseKey.setPreferredSize(new Dimension(80, 35));
		cbbStopKey.setPreferredSize(new Dimension(80, 35));
		cbbStartAfter.setPreferredSize(new Dimension(80, 35));
	}

	private void setRadioButtonProperties()
	{
		group.add(rdbtUseHotKey);
		group.add(rdbtUseTimer);

		rdbtUseHotKey.setFont(lblStartKey.getFont());
		rdbtUseHotKey.setForeground(Color.white);
		rdbtUseHotKey.setText("Use hot key");
		rdbtUseHotKey.setSelected(true);
		rdbtUseHotKey.setOpaque(false);

		rdbtUseTimer.setFont(lblStartKey.getFont());
		rdbtUseTimer.setForeground(Color.white);
		rdbtUseTimer.setText("Use timer ");
		rdbtUseTimer.setOpaque(false);
	}

	private void setButtonProperties()
	{
		buttonStart.setFont(new Font("Calibri", 0, 20));
		buttonStart.setText("Start");

		buttonCancel.setFont(buttonStart.getFont());
		buttonCancel.setText("Cancel");
	}

	private void setMainLayout()
	{
		this.add(picPanel);
		picPanel.setLayout(new FlowLayout());
		picPanel.add(panelOption);
		picPanel.add(panelButton);

		panelOption.setPreferredSize(new Dimension(690, 120));
		panelButton.setPreferredSize(new Dimension(690, 50));
		
		if(!isNewPic)
		{
			picPanel.path = "/Background/PlayOption-2.png";
			isNewPic = true;
		}
		else if(isNewPic)
		{
			picPanel.path = "/Background/PlayOption-1.png";
			isNewPic = false;
		}
		picPanel.repaint();
		
		
	}

	private void setLayoutPanelOption()
	{
		panelOption.setBorder(BorderFactory.createTitledBorder(""));
		panelOption.setOpaque(false);

		panelChoice1.setPreferredSize(new Dimension(630, 40));
		panelChoice2.setPreferredSize(new Dimension(630, 40));

		panelOption.setLayout(new FlowLayout(FlowLayout.CENTER));
		panelOption.add(panelChoice1);
		panelOption.add(panelChoice2);

		event.setEnabledComponentUseHotKey(true);
	}

	private void setLayoutPanelChoice1()
	{
		panelChoice1.setLayout(new FlowLayout(FlowLayout.LEFT));
		panelChoice1.setOpaque(false);

		panelChoice1.add(rdbtUseHotKey);
		panelChoice1.add(lblStartKey);
		panelChoice1.add(cbbStartKey);
		panelChoice1.add(lblPauseKey);
		panelChoice1.add(cbbPauseKey);
		panelChoice1.add(lblStopKey);
		panelChoice1.add(cbbStopKey);
	}

	private void setlayoutPanelChoice2()
	{
		panelChoice2.setLayout(new FlowLayout(FlowLayout.LEFT));
		panelChoice2.setOpaque(false);

		panelChoice2.add(rdbtUseTimer);
		panelChoice2.add(lblStartAfter);
		panelChoice2.add(cbbStartAfter);

	}

	private void setLayoutPanelButton()
	{
		panelButton.setLayout(new FlowLayout(FlowLayout.RIGHT));
		panelButton.setOpaque(false);
		panelButton.add(buttonStart);
		panelButton.add(buttonCancel);
	}

	/**
	 * This method will set default item in all comboBox
	 */
	private void setComboBoxModel()
	{
		String[] cbbKeyItem = { "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12" };

		cbbStartKey.setModel(model.model(cbbKeyItem));
		cbbPauseKey.setModel(model.model(cbbKeyItem));
		cbbStopKey.setModel(model.model(cbbKeyItem));

		cbbStartKey.setSelectedIndex(0);
		cbbPauseKey.setSelectedIndex(1);
		cbbStopKey.setSelectedIndex(2);

		String[] listNumber = new String[10];
		for (int i = 0; i < listNumber.length; i++)
		{
			listNumber[i] = (i + 1) + "";
		}

		cbbStartAfter.setModel(model.model(listNumber));
		cbbStartAfter.setSelectedIndex(4);
	}

	/**
	 * This method will add event for all button in this window
	 */
	private void buttonEvent()
	{
		buttonStart.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent evt)
			{
				dispose();
				registerHotKey();
				isStartPressed = true;
				frame.setState(JFrame.ICONIFIED);
			}
		});

		buttonCancel.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent evt)
			{
				isStartPressed = false;
				dispose();
			}
		});

	}

	/**
	 * This method will add event key pressed for all component in this window
	 */
	private void keyEvent()
	{
		JComboBox[] listComboBox = { cbbPauseKey, cbbStartAfter, cbbStartKey, cbbStopKey };
		JRadioButton[] listRadioButton = { rdbtUseHotKey, rdbtUseTimer };
		JButton[] listButton = { buttonCancel, buttonStart };

		for (final JComboBox comboBox : listComboBox)
		{
			comboBox.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent evt)
				{
					if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					{
						dispose();
					}
				}
			});
		}

		for (final JRadioButton radioButton : listRadioButton)
		{
			radioButton.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent evt)
				{
					if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					{
						dispose();
					}
				}
			});
		}

		for (final JButton button : listButton)
		{
			button.addKeyListener(new KeyAdapter()
			{
				@Override
				public void keyPressed(KeyEvent evt)
				{
					if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
					{
						dispose();
					}
				}
			});
		}
	}

	/**
	 * This method will add selection event to your all comboBox
	 */
	private void comboboxEvent()
	{
		JComboBox[] listComboBox = { cbbStartKey, cbbPauseKey, cbbStopKey };

		for (final JComboBox combobox : listComboBox)
		{
			combobox.addActionListener(new ActionListener()
			{

				@Override
				public void actionPerformed(ActionEvent e)
				{
					event.isSameIndexComponent(cbbStartKey, cbbPauseKey, cbbStopKey);
				}
			});
		}
	}

	/**
	 * This method will set event for 2 radioButton in this window
	 */
	private void radioButtonEvent()
	{
		rdbtUseHotKey.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				event.setEnabledComponentUseHotKey(true);
			}
		});

		rdbtUseTimer.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				event.setEnabledComponentUseHotKey(false);
			}
		});
	}

	/**
	 * This method will register your hot key to auto type
	 */
	private void registerHotKey()
	{
		JIntellitype.getInstance().unregisterHotKey(data.keyRegister(listHostKey[0]));
		JIntellitype.getInstance().unregisterHotKey(data.keyRegister(listHostKey[1]));
		JIntellitype.getInstance().unregisterHotKey(data.keyRegister(listHostKey[2]));

		listHostKey[0] = cbbStartKey.getSelectedItem().toString();
		listHostKey[1] = cbbPauseKey.getSelectedItem().toString();
		listHostKey[2] = cbbStopKey.getSelectedItem().toString();
	}
}
